package com.ryh.shirodemo.myrealm;

import com.ryh.shirodemo.model.Permission;
import com.ryh.shirodemo.model.Role;
import com.ryh.shirodemo.model.ShiroUserDto;
import com.ryh.shirodemo.model.User;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;

import java.util.*;

/**
 * @ClassName MyRealm
 * @Description 自定义realm
 * @Author RaoYH
 */
public class MyRealm extends AuthorizingRealm {
    /**
     * 用于模拟数据库
     */
    Map<String,ShiroUserDto> shiroUserMap = new HashMap<>();
    {
        super.setName("MyRealm");
        ShiroUserDto shiroUserDto = new ShiroUserDto();
        Set<String> roles = new HashSet<>();
        Set<String> permissions = new HashSet<>();
        User user = new User();
        user.setUsername("rao");
        user.setPassword("362524");

        roles.add("admin");

        permissions.add("select");
        permissions.add("delete");
        permissions.add("update");
        permissions.add("insert");

        shiroUserDto.setUser(user);
        shiroUserDto.setRoles(roles);
        shiroUserDto.setPermissions(permissions);

        shiroUserMap.put(user.getUsername(), shiroUserDto);
    }
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
        //1、从主体中传过来的认证信息中，获取用户名
        String userName = (String) principals.getPrimaryPrincipal();
        //2、通过用户名查询数据库或者缓存中获取凭证
        ShiroUserDto shiroUserDto = getUserByName(userName);
        if (shiroUserDto != null && shiroUserDto.getRoles() !=null && shiroUserDto.getPermissions() !=null){
            Set<String> roles = shiroUserDto.getRoles();
            Set<String> permissions = shiroUserDto.getPermissions();
            SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
            info.setRoles(roles);
            info.setStringPermissions(permissions);
            return info;
        }
        return null;
    }

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        //1、从主体中传过来的认证信息中，获取用户名
        String userName = (String) token.getPrincipal();
        //2、通过用户名查询数据库或者缓存中获取凭证
        ShiroUserDto shiroUserDto = getUserByName(userName);
        if (shiroUserDto != null && shiroUserDto.getUser() != null){
            User user = shiroUserDto.getUser();

            AuthenticationInfo info = new SimpleAuthenticationInfo(user.getUsername(),user.getPassword(),"MyRealm");

            return info;
        }
        return null;
    }

    /**
     * 通过userName查询User、role、permission信息
     * 正式开发时，这里查询用户修改为查询数据库
     * @param userName
     * @return
     */
    private ShiroUserDto getUserByName(String userName) {

        return shiroUserMap.get(userName);
    }
}
